<template>
  <Modal :value.sync="auditDialogVisible"
        @on-cancel="handleCloseDialog"
        title="审核单据">
    <Form ref="formData"
          :model="formData"
          :label-width="100">
      <span>请填写审核意见：</span>
      <Input v-model="formData.auditOpinion" type="textarea" :rows="5" :autosize="{maxRows: 5, minRows: 5}" placeholder="请填写审核意见" />
    </Form>
    <div slot="footer">
      <Button type="primary"
              :loading="passLoading"
              @click.stop="handleAgree()">
        <span v-if="!passLoading">审核通过</span>
        <span v-else>Loading...</span>
      </Button>
      <Button type="default"
              :loading="rejectLoading"
              @click.stop="handleRejected()">
        <span v-if="!rejectLoading">审核驳回</span>
        <span v-else>Loading...</span>
      </Button>
    </div>
  </Modal>
</template>
<script>
import { ApiRequest } from '@api/basePage'
import dynamictableactions from '@/mixins/dynamictableactions'
export default {
  mixins: [dynamictableactions],
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      auditDialogVisible: this.visible,
      passLoading: false,
      rejectLoading: false,
      formData: {
        auditOpinion: ''
      }
    }
  },
  watch: {
    /* 监听是否显示模态框的父组件传值 */
    visible (newValue) {
      this.auditDialogVisible = newValue
    },
  },
  methods: {
    /* 审核通过 */
    handleAgree () {
      let recordNo = this.$route.query.recordNo ? this.$route.query.recordNo : ''
      let data = { recordNo, auditNote: this.formData.auditOpinion }
      let initApiData = {
        url: '/sysback/product/update/record/auditOkByRecordNo',
        type: 'post'
      }
      let apiData = this.apiRequestData ? this.apiRequestData : initApiData
      let { url, type } = apiData
      this.passLoading = true
      ApiRequest(`${url}?recordNo=${recordNo}`, type, data).then((res) => {
        this.passLoading = false;
        if (res.retStatus === '1') {
          this.$Message.success('审核通过，操作完毕')
          this.handleCloseDialog()
          this.actionsChange()
          this.$router.push({ name: 'chageFromManageList' })
        }
      }).catch((err) => {
        this.$Message.error(err)
        this.passLoading = false
      })
    },
    /* 审核驳回 */
    handleRejected () {
      if (this.formData.auditOpinion) {
        let recordNo = this.$route.query.recordNo ? this.$route.query.recordNo : ''
        let data = { recordNo, auditNote: this.formData.auditOpinion }
        let initApiData = {
          url: '/sysback/product/update/record/auditFailByRecordNo',
          type: 'post'
        }
        let apiData = this.apiRequestData ? this.apiRequestData : initApiData
        let { url, type } = apiData
        this.rejectLoading = true
        ApiRequest(`${url}?recordNo=${recordNo}`, type, data).then((res) => {
          this.rejectLoading = false
          if (res.retStatus === '1') {
            this.$Message.success('审核驳回，操作完毕')
            this.handleCloseDialog()
            this.actionsChange()
            this.$router.push({ name: 'chageFromManageList' })
          }
        }).catch((err) => {
          this.$Message.error(err)
          this.rejectLoading = false
        })
      } else {
        this.$Message.warning('审核意见不能为空！请填写后再驳回')
      }
    },
    /* 关闭模态框 */
    handleCloseDialog () {
      this.auditDialogVisible = false
      this.$emit('update:visible', false)
    }
  },
}
</script>
